﻿#region codejockey.net Copyright Banner

/***************************************************************\
 * Copyright 2010 codejockey.net.                                *
 *                                                               *
 * This code is property of codejockey.net and cannot be used    *
 * without explicit permission given by a codejockey.net         *
 * Representative.  For details contact: source@codejockey.net   *
 \***************************************************************/

#endregion

#region Using Clause

using codejockey.BingPaper.Utils.ReplacementTokens;
using codejockey.Shared.Text.ReplacementTokens;

#endregion

namespace codejockey.BingPaper.Utils
{
  /// <summary>
  /// The TokenManager class can hold tokens and can process a string using
  /// all of them via the TokenProcesser class.
  /// </summary>
  public static class TokenManager
  {
    private const string DefaultFormat = "{0:00}";

    private static TokenList _tokens = new TokenList();

    /// <summary>
    /// Gets or sets the tokens.
    /// </summary>
    /// <value>The tokens.</value>
    public static TokenList Tokens
    {
      get
      {
        //if (tokens.Count == 0)
        //  GetDefaultTokens();

        return _tokens;
      }

      set { _tokens = value; }
    }

    /// <summary>
    /// Gets or sets the string format.
    /// </summary>
    /// <value>The string format.</value>
    private static string FormatString { get; set; }

    private static void GetDefaultTokens()
    {
      _tokens = new TokenList();

      var format = DefaultFormat;

      if (!string.IsNullOrWhiteSpace(FormatString))
        format = FormatString;
      
      var monthToken = new MonthReplacementToken(format);
      var dayToken = new DayReplacementToken(format);
      var yearToken = new YearReplacementToken(format);
      var hourToken = new HourReplacementToken(format);
      var minuteToken = new MinuteReplacementToken(format);
      var secondToken = new SecondReplacementToken(format);
      var milisecondToken = new MillisecondReplacementToken(format);
      var domainToken = new DomainReplacementToken();
      var userToken = new CurrentUserReplacementToken();
      var userDomainToken = new CurrentUserWithDomainReplacementToken();

      _tokens.Add(monthToken.Token, monthToken);
      _tokens.Add(dayToken.Token, dayToken);
      _tokens.Add(yearToken.Token, yearToken);
      _tokens.Add(hourToken.Token, hourToken);
      _tokens.Add(minuteToken.Token, minuteToken);
      _tokens.Add(milisecondToken.Token, milisecondToken);
      _tokens.Add(secondToken.Token, secondToken);
      _tokens.Add(domainToken.Token, domainToken);
      _tokens.Add(userToken.Token, userToken);
      _tokens.Add(userDomainToken.Token, userDomainToken);
    }

    /// <summary>
    /// Loads the tokens.
    /// </summary>
    public static void LoadTokens()
    {
      _tokens.Clear();
      GetDefaultTokens();
    }

    /// <summary>
    /// Parses the specified tokenized string.
    /// </summary>
    /// <param name="tokenizedString">The tokenized string.</param>
    /// <returns></returns>
    public static string Parse(string tokenizedString)
    {
      var processor = new TokenProcessor(Tokens);

      return processor.Parse(tokenizedString);
    }
  }
}